﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neovolve.BlogEngine.Web;

namespace Neovolve.BlogEngine.Web.UnitTests
{
    /// <summary>
    ///This is a test class for CSUrlRedirectorTests and is intended
    ///to contain all CSUrlRedirectorTests Unit Tests
    ///</summary>
    [TestClass]
    public class CSUrlRedirectorTests
    {
        /// <summary>
        ///A test for StripIllegalCharacters
        ///</summary>
        [TestMethod]
        [DeploymentItem("Neovolve.BlogEngine.Web.dll")]
        public void StripIllegalCharactersTest()
        {
            RunStripIllegalCharactersTest(
                "Visual Studio Addin - No such interface supported", "visual studio addin no such interface supported");
            RunStripIllegalCharactersTest("Visual Studio Tip: Escape without fear", "visual studio tip escape without fear");
            RunStripIllegalCharactersTest("Example of coding for testability", "example of coding for testability");
            RunStripIllegalCharactersTest("Trojan in VS2008 SP1?", "trojan in vs2008 sp1");
            RunStripIllegalCharactersTest(
                "Things to look at when tracing does not output data",
                "things to look at when tracing does not output data");
            RunStripIllegalCharactersTest("AOP in .NET", "aop in net");
            RunStripIllegalCharactersTest("Neovolve ReSharper Plugins 1.0 released", "neovolve resharper plugins 1 0 released");
            RunStripIllegalCharactersTest(
                "Reflection Pop Quiz - Does interface inheritance exist?",
                "reflection pop quiz does interface inheritance exist");
            RunStripIllegalCharactersTest("Installing WiX 3.0 - Votive on Vista x64", "installing wix 3 0 votive on vista x64");
            RunStripIllegalCharactersTest("Load tests don't like code coverage", "load tests don t like code coverage");
            RunStripIllegalCharactersTest("Outlook has a dialog open, but it doesn't", "outlook has a dialog open but it doesn t");
            RunStripIllegalCharactersTest(
                "US ISV Developer Evangelism Team : Free Software, Students and Technology",
                "us isv developer evangelism team free software students and technology");
            RunStripIllegalCharactersTest(
                "Locking/Thread Synchronization Performance Question",
                "locking thread synchronization performance question");
            RunStripIllegalCharactersTest("VS2008 read only automatic properties", "vs2008 read only automatic properties");
            RunStripIllegalCharactersTest(
                "The pain and rudeness of Windows Live (Writer)", "the pain and rudeness of windows live writer");
            RunStripIllegalCharactersTest("Blog != RSS", "blog rss");
            RunStripIllegalCharactersTest(
                "Free digital certificates from StartCom/StartSSL", "free digital certificates from startcom startssl");
            RunStripIllegalCharactersTest("It's a boy!", "it s a boy");
            RunStripIllegalCharactersTest(
                "Be careful of code clean up's when you have WF projects",
                "be careful of code clean up s when you have wf projects");
            RunStripIllegalCharactersTest(
                "So you still can't have a path more than 260 characters???",
                "so you still can t have a path more than 260 characters");
            RunStripIllegalCharactersTest("Dave's CAPTCHA deserves some link love", "dave s captcha deserves some link love");
            RunStripIllegalCharactersTest("WPF book: Applications = Code + Markup", "wpf book applications code markup");
            RunStripIllegalCharactersTest("Did I ignore you? I didn't mean too....", "did i ignore you i didn t mean too");
            RunStripIllegalCharactersTest("Tech.Coffee", "tech coffee");
            RunStripIllegalCharactersTest("Does-this-mean-I-am-getting-old_3F00_", "does this mean i am getting old");
            RunStripIllegalCharactersTest("WPF-book_3A00_-Applications-_3D00_-Code-_2B00_-Markup", "wpf book applications code markup");
            RunStripIllegalCharactersTest("Did-I-ignore-you_3F00_-I-didn_2700_t-mean-too_2E002E002E002E00_", "did i ignore you i didn t mean too");
        }

        #region Static Helper Methods

        /// <summary>
        /// Runs the get CS slug test.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="expected">The expected.</param>
        public static void RunStripIllegalCharactersTest(String text, String expected)
        {
            String actual = CSUrlRedirector_Accessor.StripIllegalCharacters(text);

            Assert.AreEqual(expected, actual, "StripIllegalCharacters returned an incorrect value");
        }

        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get;
            set;
        }

        /// <summary>
        ///A test for GetUrlFormat
        ///</summary>
        [TestMethod]
        [DeploymentItem("Neovolve.BlogEngine.Web.dll")]
        public void GetUrlFormatTest()
        {
            // Test the empty cases
            RunGeturlFormatTest(null, CSUrlFormat.Unknown);
            RunGeturlFormatTest(String.Empty, CSUrlFormat.Unknown);

            // Test unknown urls
            RunGeturlFormatTest("http://somedomain.com", CSUrlFormat.Unknown);
            RunGeturlFormatTest("http://somedomain.com/default.aspx", CSUrlFormat.Unknown);
            RunGeturlFormatTest("http://somedomain.com/post/2008/09/04/test.aspx", CSUrlFormat.Unknown);

            // Test for day post list
            RunGeturlFormatTest("http://somedomain.com/archive/2008/04/02.aspx", CSUrlFormat.DayPostList);

            // Test for month post list
            RunGeturlFormatTest("http://somedomain.com/archive/2008/04.aspx", CSUrlFormat.MonthPostList);

            // test for tag post list
            RunGeturlFormatTest("http://somedomain.com/archive/tags/Some+Tag/default.aspx", CSUrlFormat.TagPostList);
            RunGeturlFormatTest("http://somedomain.com/archive/tags/SomeTag/Another+Tag/default.aspx", CSUrlFormat.TagPostList);

            // Test for a post
            RunGeturlFormatTest("http://somedomain.com/archive/2008/04/12/Some-title.aspx", CSUrlFormat.Post);

            // Test for a page
            RunGeturlFormatTest("http://somedomain.com/pages/Some-Page-title.aspx", CSUrlFormat.Page);

            // Test for an rss feed
            RunGeturlFormatTest("http://somedomain.com/rss.aspx", CSUrlFormat.Rss);
            RunGeturlFormatTest("http://somedomain.com/rss.aspx?Tags=Some+Tag&AndTags=1", CSUrlFormat.Rss);
            RunGeturlFormatTest("http://somedomain.com/rss.aspx?Tags=Some+Tag/AnotherTag&AndTags=1", CSUrlFormat.Rss);

            // Test for an atom feed
            RunGeturlFormatTest("http://somedomain.com/atom.aspx", CSUrlFormat.Atom);
            RunGeturlFormatTest("http://somedomain.com/atom.aspx?Tags=Some+Tag&AndTags=1", CSUrlFormat.Atom);
            RunGeturlFormatTest("http://somedomain.com/atom.aspx?Tags=Some+Tag/AnotherTag&AndTags=1", CSUrlFormat.Atom);
        }

        /// <summary>
        /// Runs the geturl format test.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="expected">The expected.</param>
        private static void RunGeturlFormatTest(string url, CSUrlFormat expected)
        {
            CSUrlFormat actual = CSUrlRedirector_Accessor.GetUrlFormat(url);
            Assert.AreEqual(expected, actual, "GetUrlFormat returned an incorrect value");
        }
    }
}